package com.yxx.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxx.business.entity.*;
import com.yxx.business.service.PhaseService;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbOperationPhaseMapper;
import com.yxx.business.service.ITbOperationPhaseService;

/**
 * 运营维护阶段Service业务层处理
 *
 * @author yxx
 * @date 2025-08-16
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbOperationPhaseServiceImpl extends ServiceImplPlus<TbOperationPhaseMapper, TbOperationPhaseDo> implements ITbOperationPhaseService, PhaseService {

    private final TbOperationPhaseMapper tbOperationPhaseMapper;

    /**
     * 查询运营维护阶段分页结果
     *
     * @param tbOperationPhaseQueryDto 运营维护阶段查询实体
     * @return 运营维护阶段分页
     */
    @Override
    public PageResult<TbOperationPhaseVo> selectTbOperationPhaseVoPage(TbOperationPhaseQueryDto tbOperationPhaseQueryDto) {
        startPage();
        PageResult<TbOperationPhaseVo> page
                = super.getMyBatisPageResult(selectTbOperationPhaseDoList(tbOperationPhaseQueryDto), TbOperationPhaseVo.class);
        clearPage();
        return page;
    }

    /**
     * 查询运营维护阶段Vo列表
     *
     * @param tbOperationPhaseQueryDto 运营维护阶段查询实体
     * @return 运营维护阶段集合
     */
    @Override
    public List<TbOperationPhaseVo> selectTbOperationPhaseVoList(TbOperationPhaseQueryDto tbOperationPhaseQueryDto) {
        return super.convertVoList(tbOperationPhaseMapper.selectTbOperationPhaseList(tbOperationPhaseQueryDto), TbOperationPhaseVo.class);
    }

    /**
     * 查询运营维护阶段Do列表
     *
     * @param tbOperationPhaseQueryDto 运营维护阶段查询实体
     * @return 运营维护阶段集合
     */
    @Override
    public List<TbOperationPhaseDo> selectTbOperationPhaseDoList(TbOperationPhaseQueryDto tbOperationPhaseQueryDto) {
        return tbOperationPhaseMapper.selectTbOperationPhaseList(tbOperationPhaseQueryDto);
    }

    /**
     * 查询单个运营维护阶段
     *
     * @param operationId 运营维护阶段主键
     * @return 运营维护阶段单个
     */
    @Override
    public TbOperationPhaseVo selectTbOperationPhaseVoByOperationId(Long operationId) {
        return super.convertBean(tbOperationPhaseMapper.selectTbOperationPhaseByOperationId(operationId), TbOperationPhaseVo.class);
    }

    /**
     * 新增运营维护阶段
     *
     * @param tbOperationPhaseQueryDto 运营维护阶段编辑实体
     * @return 结果
     */
    @Override
    public int insertTbOperationPhase(TbOperationPhaseEditDto tbOperationPhaseQueryDto) {
        tbOperationPhaseQueryDto.setCreateById(getUserIdOrNotLogged());
        tbOperationPhaseQueryDto.setCreateBy(getUserNameOrNotLogged());
        tbOperationPhaseQueryDto.setCreateTime(DateUtils.getNowDate());
        TbOperationPhaseDo tbOperationPhaseDo = super.convertT(tbOperationPhaseQueryDto);
        tbOperationPhaseMapper.insertTbOperationPhase(tbOperationPhaseDo);
        return tbOperationPhaseDo.getOperationId().intValue();
    }

    /**
     * 修改运营维护阶段
     *
     * @param operationId 运营维护阶段主键
     * @param tbOperationPhaseQueryDto 运营维护阶段编辑实体
     * @return 结果
     */
    @Override
    public int updateTbOperationPhase(Long operationId, TbOperationPhaseEditDto tbOperationPhaseQueryDto) {
        TbOperationPhaseVo tbOperationPhaseVo = selectTbOperationPhaseVoByOperationId(operationId);
        if (tbOperationPhaseVo.getOperationId() == null) {
            return insertTbOperationPhase(tbOperationPhaseQueryDto);
        } else {
            tbOperationPhaseQueryDto.setUpdateBy(getUserNameOrNotLogged());
            tbOperationPhaseQueryDto.setUpdateTime(DateUtils.getNowDate());
            TbOperationPhaseDo tbOperationPhaseDo = super.convertT(tbOperationPhaseQueryDto);
            tbOperationPhaseMapper.updateTbOperationPhase(tbOperationPhaseDo.setOperationId(operationId));
            return tbOperationPhaseDo.getOperationId().intValue();
        }
    }

    /**
     * 批量删除运营维护阶段
     *
     * @param operationIds 需要删除的运营维护阶段主键集合
     * @return 结果
     */
    @Override
    public int deleteTbOperationPhaseByOperationIds(List<Long> operationIds) {
        return tbOperationPhaseMapper.deleteTbOperationPhaseByOperationIds(operationIds);
    }

    /**
     * 删除单个运营维护阶段信息
     *
     * @param operationId 运营维护阶段主键
     * @return 结果
     */
    @Override
    public int deleteTbOperationPhaseByOperationId(Long operationId) {
        return tbOperationPhaseMapper.deleteTbOperationPhaseByOperationId(operationId);
    }

    @Override
    public TbEquipmentPhaseVo getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return convertBean(tbOperationPhaseMapper.selectOne(
                        new LambdaQueryWrapper<TbOperationPhaseDo>().eq(TbOperationPhaseDo::getEngineeringId, tbEquipmentPhaseDto.getEngineeringId()).eq(TbOperationPhaseDo::getProjectId, tbEquipmentPhaseDto.getProjectId())),
                TbEquipmentPhaseVo.class);
    }

    @Override
    public int savePhase(TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        Long l = tbOperationPhaseMapper.selectCount(new LambdaQueryWrapper<TbOperationPhaseDo>().eq(TbOperationPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()));
        if (l > 0) {
            tbOperationPhaseMapper.update(convertT(tbEquipmentPhaseVo), new LambdaUpdateWrapper<TbOperationPhaseDo>().eq(TbOperationPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()).eq(TbOperationPhaseDo::getProjectId, tbEquipmentPhaseVo.getProjectId()));
        } else {
            tbOperationPhaseMapper.insert(convertT(tbEquipmentPhaseVo));
        }
        return 1;
    }

    @Override
    public int updatePhase(Long id, TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return tbOperationPhaseMapper.updateById(convertT(tbEquipmentPhaseVo).setOperationId(id));
    }
}
